 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Pagequery_model extends CI_Model {
	public $debug = false;
	public function __construct(){

    }

    public function page_query($_db,$table,$order,$rows,$where,$nowpage,$rows_count='Total'){
        if(!empty($order))$order=	" order by ".$order;
        if(empty($where))$where	=	" 1=1 ";

		$sqlstr		=	'SELECT COUNT(1) as Total FROM '.$table." where  ".$where;
		if($this->debug){
			echo $sqlstr."<br/>";
		}
		$query		=	$_db->query($sqlstr);
		$total		=	0;
		if ($query->row()) {
			$page_row	=	$query->row_array();
			$total		=	$page_row['Total'];
		}

	    $nowpage	=	$nowpage-1;
        $sqlstr		=	'SELECT *,'.$total.' as Total FROM '.$table." where  ".$where .$order.' limit '.$nowpage*$rows.','.$rows.'';
		if($this->debug){
			echo $sqlstr."<br/>";
		}
        $query		=	$_db->query($sqlstr); 
		$page_data	=	array();
		if ($query->result()) {
			$page_data	=	$query->result_array();
		}
		return $page_data;
    }
    
    public function page_query_sql($_db,$sql,$rows,$nowpage){
		$sqlstr		=	'SELECT COUNT(1) as Total FROM ('.$sql.') Main ';
		if($this->debug){
			echo $sqlstr."<br/>";
		}
		$query		=	$_db->query($sqlstr);
		$total		=	0;
		if ($query->row()) {
			$page_row	=	$query->row_array();
			$total		=	$page_row['Total'];
		}

	    $nowpage	=	$nowpage-1;
        $sqlstr		=	$sql.' limit '.$nowpage*$rows.','.$rows.'';
		if($this->debug){
			echo $sqlstr."<br/>";
		}
        $query		=	$_db->query($sqlstr); 
		$page_data	=	array();
		if ($query->result()) {
			$page_data	=	$query->result_array();
		}
		return $page_data;
    }
}